import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useStore as useUserStore } from "@/store/user";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "achievement",
    component: () => import("@/views/achievement/achievement"),
    meta: {
      title: "业绩看板",
    },
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login"),
    meta: {
      title: "宜春惠民保业务员在线-登录",
    },
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
// 路由跳转前判断权限
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore();
  const token = userStore.token;
  if(token)
  {
     next();
     return;
  }
  else
  {
     if(to.name == "login")
     {
        next();
        return;
     }
     else
     {
         next("/login");
    //  next('login');
        return;
     }
  }
})

export default router
